package net.cyue.homework.student.management.servlet.user;

import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpSession;
import net.cyue.homework.student.management.http.HTTPServlet;
import net.cyue.nest.nestjava.common.interfaces.http.IHTTPRequest;
import net.cyue.nest.nestjava.common.interfaces.http.IHTTPResponse;
import net.cyue.nest.nestjava.platform.tomcat.TomcatHTTPResponse;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Random;

@WebServlet(name = "UserCodeServlet", value = "/action/user/code")
public class UserCodeServlet extends HTTPServlet {

    private static final int CODE_LENGTH = 4;
    private static final int CODE_WIDTH = 70;
    private static final int CODE_HEIGHT = 30;
    private static final String[] CODE_CHARS = {
        "0", "1", "2", "3", "4", "5", "6", "7", "8", "9",
        "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M",
        "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z",
        "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m",
        "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"
    };
    private static final int CODE_FONT_SIZE = 16;

    public String generateCode() {
        StringBuilder code = new StringBuilder();
        for (int i = 0; i < CODE_LENGTH; i++) {
            int index = (int) (Math.random() * CODE_CHARS.length);
            code.append(CODE_CHARS[index]);
        }
        return code.toString();
    }

    public BufferedImage generateCodeImage(String code) {
        BufferedImage image = new BufferedImage(CODE_WIDTH, CODE_HEIGHT, BufferedImage.TYPE_INT_RGB);
        Graphics2D g = (Graphics2D) image.getGraphics();
        g.setColor(Color.WHITE);
        g.setFont(new Font("Arial", Font.BOLD, CODE_FONT_SIZE));
        g.fillRect(0, 0, image.getWidth(), image.getHeight());
        for (int i = 0; i < CODE_LENGTH; i++) {
            int red = (int) (Math.random() * 255);
            int green = (int) (Math.random() * 255);
            int blue = (int) (Math.random() * 255);
            g.setColor(new Color(red, green, blue));
            g.drawString(
                code.substring(i, i + 1),
                i * CODE_FONT_SIZE + 4,
                (int) (image.getHeight() / 1.5)
            );
        }
        g.dispose();
        return image;
    }

    @Override
    public void doGet(
        IHTTPRequest request,
        IHTTPResponse response
    ) throws IOException {
        TomcatHTTPResponse tResponse = (TomcatHTTPResponse) response;
        HttpSession session = request.getSession();
        String code = this.generateCode();
        session.setAttribute("code", code);
        System.out.println("验证码：" + code);

        BufferedImage image = this.generateCodeImage(code);
        response.putHeader("Content-Type", "image/png");
        try {
            OutputStream out = tResponse.getOutputStream();
            ImageIO.write(image, "png", out);
        } catch (IOException e) {
            e.printStackTrace();
            response.putHeader("Content-Type", "text/html;charset=utf-8");
            response.end(e.getMessage());
        }
    }
}


